import Vue from "vue";
import Router from "vue-router";
import api from "@/utils/api/index";

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require("./import-" + process.env.NODE_ENV);

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
	// {
	// 	path: "/404",
	// 	component: _import("common/404"),
	// 	name: "404",
	// 	meta: { title: "404未找到" }
	// },
	{
		path: "/login",
		component: _import("login/index"),
		name: "login",
		meta: { title: "登录" }
	}
	// {
	// 	path: "/",
	// 	component: _import("login/index"),
	// 	name: "login",
	// 	meta: {
	// 		title: "登录"
	// 	},
	// 	redirect: "/login"
	// },
	// {
	// 	path: "/login",
	// 	component: _import("login/index"),
	// 	name: "login",
	// 	meta: {
	// 		title: "登录"
	// 	}
	// }
];
// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
	path: "/",
	component: _import("main/index"),
	redirect: {
		name: "home"
	},
	name: "main",
	meta: {
		title: "主入口整体布局"
	},
	children: [
		{
			path: "/home",
			component: _import("home/index"),
			meta: {
				title: "首页"
			},
			name: "home",
			resourceName: "首页", 
			url: "home/index"
		}
	// {
	// 	path: "/sys-menu",
	// 	component: _import("system/sys-menu"),
	// 	meta: {
	// 		title: "菜单管理"
	// 	},
	// 	name: "sys-menu"
	// },
	// {
	// 	path: "/sys-role",
	// 	component: _import("system/sys-role"),
	// 	meta: {
	// 		title: "角色管理"
	// 	},
	// 	name: "sys-role"
	// },
	// {
	// 	path: "/sys-user",
	// 	component: _import("system/sys-user"),
	// 	meta: {
	// 		title: "账号管理"
	// 	},
	// 	name: "sys-user"
	// }
	]
};

const router = new Router({
	mode: "hash",
	// 解决vue框架页面跳转有白色不可追踪色块的bug
	scrollBehavior: () => ({
		y: 0
	}),
	isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
	routes: globalRoutes.concat(mainRoutes)
});

router.beforeEach((to, from, next) => {
	var isLogin = sessionStorage.getItem('userInfo');
	if (isLogin) {
		console.log(555)
		// api.callName.asidemenuList().then(res => {
		// 			if (res.data.code === 0) {
		// 				res.data.data.map(item => {
		// 					var obj = {};
		// 					obj.title = item.resourceName
		// 					item.path = '/system-sys-menu',
		// 					item.name = 'system-sys-menu',
		// 					item.url = 'system/sys-menu',
		// 					item.meta = obj,
		// 					item.component = ()=> import(`@/views/${item.url}`)
		// 				})
						
		// 				sessionStorage.setItem(
		// 					"menuList",
		// 					JSON.stringify(res.data.data || "[]")
		// 				);					
		// 				mainRoutes.children = mainRoutes.children.concat(res.data.data);
		// 				router.addRoutes([mainRoutes]);
		// 				router.options.isAddDynamicMenuRoutes = true;
		// 				next();
		// 			}
		// 		}).catch(e => {
		// 			console.log(e)
		// 			console.log("请求菜单列表和权限失败,，跳转至登录页！！");
		// 			next({path:'/login'})
		// 		});
		// next();
		if(!(sessionStorage.getItem('menuList'))) {
			console.log(777)
			api.callName.asidemenuList().then(res => {
				if (res.data.code === 0) {
					res.data.data.map(item => {
						var obj = {};
						obj.title = item.resourceName
						item.path = '/system-sys-menu',
						item.name = 'system-sys-menu',
						item.url = 'system/sys-menu',
						item.meta = obj,
						item.component = ()=> import(`@/views/${item.url}`)
					})
					
					sessionStorage.setItem(
						"menuList",
						JSON.stringify(res.data.data || "[]")
					);					
					mainRoutes.children = mainRoutes.children.concat(res.data.data);
					router.addRoutes([mainRoutes]);
					router.options.isAddDynamicMenuRoutes = true;
					next();
				}
			}).catch(e => {
				console.log(e)
				console.log("请求菜单列表和权限失败,，跳转至登录页！！");
				next({path:'/login'})
			});
		} else {
			var list = JSON.parse(sessionStorage.getItem('menuList'));
			list.map(item => {
				var obj = {};
				obj.title = item.resourceName
				item.path = '/system-sys-menu',
				item.name = 'system-sys-menu',
				item.url = 'system/sys-menu',
				item.meta = obj,
				item.component = ()=> import(`@/views/${item.url}`)
			})
			mainRoutes.children = mainRoutes.children.concat(list);
					router.addRoutes([mainRoutes]);
					router.options.isAddDynamicMenuRoutes = true;
			next();

		}
		next()
		console.log(888)
		
		
	} else {
		console.log(666)
		// next({path:'/login'})
		// next()
		if(to.name == 'login') {
			next()
		}
		else {
			next('/login')
		}
	}
});
export default router;
